package com.tongtong.middleware.blockchain.client.web3j.contract;

import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.DynamicBytes;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.generated.Uint256;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ERC1155Input {
    /**
     * 铸造NFT(需要管理权限用户操作)
     * function mint(address to_, uint256 id_, uint256 amount_)
     */
    public static String mint(String to, BigInteger id, BigInteger amount) {
        Function function = new Function("mint",
                Arrays.asList(new Address(to), new Uint256(id), new Uint256(amount)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 批量铸造NFT(需要管理权限用户操作)
     * function mintBatch(address to, uint256[] memory ids, uint256[] memory values)
     */
    public static String mintBatch(String to, List<BigInteger> ids, List<BigInteger> values) {
        Uint256[] tokenIDs = ids.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> idsDynamicArray = new DynamicArray<>(Uint256.class, tokenIDs);
        Uint256[] tokenValues = values.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> valuesDynamicArray = new DynamicArray<>(Uint256.class, tokenValues);
        Function function = new Function("mintBatch",
                Arrays.asList(new Address(to), idsDynamicArray, valuesDynamicArray),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 销毁NFT(需要管理权限用户操作)
     * function burn(address account_, uint256 id_, uint256 amount_)
     */
    public static String burn(String account, BigInteger id, BigInteger amount) {
        Function function = new Function("burn",
                Arrays.asList(new Address(account), new Uint256(id), new Uint256(amount)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 批量销毁NFT(需要管理权限用户操作)
     * function burnBatch(address from, uint256[] memory ids, uint256[] memory values)
     */
    public static String burnBatch(String from, List<BigInteger> ids, List<BigInteger> values) {
        Uint256[] tokenIDs = ids.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> idsDynamicArray = new DynamicArray<>(Uint256.class, tokenIDs);
        Uint256[] tokenValues = values.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> valuesDynamicArray = new DynamicArray<>(Uint256.class, tokenValues);
        Function function = new Function("burnBatch",
                Arrays.asList(new Address(from), idsDynamicArray, valuesDynamicArray),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 获取授权(需要管理权限用户操作)
     * function approval(address account_)
     */
    public static String approval(String account) {
        Function function = new Function("approval",
                List.of(new Address(account)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 转移NFT(需要NFT归属用户操作)
     * function safeTransferFrom(address from, address to, uint256 id, uint256 amount, bytes memory data)
     */
    public static String safeTransferFrom(String from, String to, BigInteger id, BigInteger num, String data) {
        Function function = new Function("safeTransferFrom",
                Arrays.asList(new Address(from), new Address(to), new Uint256(id), new Uint256(num), new DynamicBytes(data.getBytes())),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 批量转移NFT(需要NFT归属用户操作)
     * function safeBatchTransferFrom(address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
     */
    public static String safeBatchTransferFrom(String from, String to, List<BigInteger> ids, List<BigInteger> amounts, String data) {
        Uint256[] tokenIDs = ids.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> idsDynamicArray = new DynamicArray<>(Uint256.class, tokenIDs);
        Uint256[] tokenAmounts = amounts.stream().map(Uint256::new).toArray(Uint256[]::new);
        DynamicArray<Uint256> amountsDynamicArray = new DynamicArray<>(Uint256.class, tokenAmounts);
        Function function = new Function("safeBatchTransferFrom",
                Arrays.asList(new Address(from), new Address(to), idsDynamicArray, amountsDynamicArray, new DynamicBytes(data.getBytes())),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 查询指定用户拥有指定TokenID的数量
     * function balanceOf(address account, uint256 id)
     */
    public static String balanceOf(String account, BigInteger id) {
        Function function = new Function("balanceOf",
                Arrays.asList(new Address(account), new Uint256(id)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 查询用户是否被指定操作者授权
     * function isApprovedForAll(address account, address operator)
     */
    public static String isApprovedForAll(String account, String operator) {
        Function function = new Function("isApprovedForAll",
                Arrays.asList(new Address(account), new Address(operator)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
    /**
     * 查询指定TokenID的TokenURI
     * function uri(uint256 id_)
     */
    public static String uri(BigInteger tokenId) {
        Function function = new Function("uri",
                List.of(new Uint256(tokenId)),
                Collections.singletonList(null));
        return FunctionEncoder.encode(function);
    }
}
